﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Model;
using Common;
using MySql.Data.MySqlClient;

namespace DAL
{
   public class ArcYunWebDAL
    {
       #region 首页banner列表
       public IList<SOP_ArcyunHomePic> GetHomePicList()
       {
           using (MySqlConnection conn = SqlHelper.Settings.GetSOPMySqlConnection(false))
           {
               try
               {
                   string SQL = "select ID,Title,ImgUrl,Href,Remark,Sort,Status,AddTime,LastUpdateTime from SOP_ArcyunHomePic where Status=5 order by Sort ASC;";
                   MySqlCommand cmd = new MySqlCommand(SQL, conn);
                   cmd.CommandType = CommandType.Text;

                   if (conn.State == ConnectionState.Closed)
                   {
                       conn.Open();
                   }
                   IList<SOP_ArcyunHomePic> List = new List<SOP_ArcyunHomePic>();

                   using (MySqlDataReader sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                   {
                       while (sdr.Read())
                       {
                           SOP_ArcyunHomePic Ent = new SOP_ArcyunHomePic();

                           Ent.ID = Convert.ToInt32(sdr["ID"]);
                           Ent.Sort = Convert.ToInt32(sdr["Sort"]);
                           Ent.Status = Convert.ToInt32(sdr["Status"]);
                           Ent.Title = Convert.ToString(sdr["Title"]);
                           Ent.AddTime = Convert.ToDateTime(sdr["AddTime"]);
                           Ent.LastUpdateTime = Convert.ToDateTime(sdr["LastUpdateTime"]);
                           Ent.Href = Convert.ToString(sdr["Href"]);
                           Ent.Remark = Convert.ToString(sdr["Remark"]);
                           Ent.ImgUrl = Convert.ToString(sdr["ImgUrl"]);

                           List.Add(Ent);
                       }
                   }

                   return List;
               }
               catch (Exception ex)
               {
                   return null;
               }
               finally
               {
                   conn.Close();
               }
           }
       }
       #endregion
              
        #region 媒体报道

        public SOP_ArcyunNews GetNewsReport(string NewId)
        {
            using (MySqlConnection conn = SqlHelper.Settings.GetSOPMySqlConnection(false))
            {
                try
                {
                    string SQL = @"select NewId,Title,Author,Summary,CoverIMG,IsDisplayCoverIMG,Des,OriginalUrl,AddTime,ViewCount  
                    from SOP_ArcyunNews
                    where NewId = @NewID_Para;";
                    MySqlCommand cmd = new MySqlCommand(SQL, conn);
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.Add("@NewID_Para", MySqlDbType.VarChar, 10).Value = NewId;

                    if (conn.State == ConnectionState.Closed)
                    {
                        conn.Open();
                    }
                    using (MySqlDataReader sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                    {
                        while (sdr.Read())
                        {
                            SOP_ArcyunNews Ent = new SOP_ArcyunNews();

                            Ent.NewId = Convert.ToInt32(sdr["NewId"]);
                            Ent.Title = Convert.ToString(sdr["Title"]);
                            Ent.Author = Convert.ToString(sdr["Author"]);
                            Ent.Summary = Convert.ToString(sdr["Summary"]);
                            Ent.CoverIMG = Convert.ToString(sdr["CoverIMG"]);
                            Ent.IsDisplayCoverIMG = Convert.ToInt32(sdr["IsDisplayCoverIMG"]);
                            Ent.Des = Convert.ToString(sdr["Des"]);
                            Ent.OriginalUrl = Convert.ToString(sdr["OriginalUrl"]);
                            Ent.AddTime = Convert.ToDateTime(sdr["AddTime"]);
                            if (!string.IsNullOrWhiteSpace(sdr["ViewCount"].ToString()))
                                Ent.ViewCount = Convert.ToInt32(sdr["ViewCount"].ToString());
                            else
                                Ent.ViewCount = 1;
                            return Ent;
                        }
                    }
                    return null;
                }
                catch (Exception ex)
                {
                    return null;
                }
                finally
                {
                    conn.Close();
                }
            }
        }

       /// <summary>
       ///获取媒体报道列表
       /// </summary>
       /// <param name="rows"></param>
       /// <param name="page"></param>
       /// <param name="totalCount"></param>
       /// <returns></returns>
        public IList<SOP_ArcyunNews> GetArcyunNewsList(int PageSize, int PageIndex, string Strwhere, out int Count)
       {
           string strGetFields = "NewId,Title,Author,Summary,CoverIMG,IsDisplayCoverIMG,Des,OriginalUrl,Status,IPAddress,AddTime";
           string key = "NewId";
           string tblName = "SOP_ArcyunNews";
           string OrderType = "NewId desc ";

           DataTable dt = SqlHelper.SqlHelper.GetSOPPageByBossID(tblName, strGetFields, key, PageSize, PageIndex, OrderType, Strwhere, out Count);

           IList<SOP_ArcyunNews> List = new List<SOP_ArcyunNews>();

           if (dt != null)
           {
               for (int i = 0; i < dt.Rows.Count; i++)
               {
                   SOP_ArcyunNews Ent = new SOP_ArcyunNews();
                   Ent.NewId = Convert.ToInt32(dt.Rows[i]["NewId"]);
                   Ent.Title = Convert.ToString(dt.Rows[i]["Title"]);
                   Ent.Author = Convert.ToString(dt.Rows[i]["Author"]);
                   Ent.Summary = Convert.ToString(dt.Rows[i]["Summary"]);
                   Ent.CoverIMG = Convert.ToString(dt.Rows[i]["CoverIMG"]);
                   Ent.IsDisplayCoverIMG = Convert.ToInt32(dt.Rows[i]["IsDisplayCoverIMG"]);
                   Ent.Des = Convert.ToString(dt.Rows[i]["Des"]);
                   Ent.OriginalUrl = Convert.ToString(dt.Rows[i]["OriginalUrl"]);
                   Ent.Status = Convert.ToInt32(dt.Rows[i]["Status"]);
                   Ent.IPAddress = Convert.ToString(dt.Rows[i]["IPAddress"]);
                   Ent.AddTime = Convert.ToDateTime(dt.Rows[i]["AddTime"]);
                   Ent.AddTimeObj = dt.Rows[i]["AddTime"].ToString().Substring(0,10);
                   List.Add(Ent);
               }
           }

           return List;
        }

       /// <summary>
       /// 阅读次数
       /// </summary>
       /// <param name="NewId"></param>
       /// <returns></returns>
        public int SetNewsViewCount(int NewId)
        {
            using (MySqlConnection conn = SqlHelper.Settings.GetSOPMySqlConnection(false))
            {
                try
                {

                    MySqlCommand cmd = new MySqlCommand("P_SetNewsViewCountById", conn);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@NewID_Para", MySqlDbType.Int32).Value = NewId;
                    cmd.Parameters.Add("@IsSuccess_Para", MySqlDbType.Int32).Value = 0;
                    cmd.Parameters["@IsSuccess_Para"].Direction = ParameterDirection.Output;

                    if (conn.State == ConnectionState.Closed)
                    {
                        conn.Open();
                    }
                    cmd.ExecuteNonQuery();
                    Object ObjRtID = cmd.Parameters["@IsSuccess_Para"].Value;
                    return ObjRtID == null || ObjRtID.ToString() == string.Empty ? -5 : (int)ObjRtID;
                }
                catch (Exception ex)
                {
                    return -1000;
                }
                finally
                {
                    conn.Close();
                }
            }
        }
        #endregion
    }
}
